<?php
/**
 * Created by PhpStorm.
 * User: wodrow
 * Date: 2018/10/19
 * Time: 11:36
 */

namespace console\members\wodrow\swoolewschat;


use yii\base\Component;

class ChatServer extends Component
{
    public $ip;
    public $port;

    public function start()
    {
        $server = new \swoole_websocket_server($this->ip, $this->port);
        $server->set([
            ''
        ]);
        $server->on('open', function (\swoole_websocket_server $server, $request) {
            echo "server: handshake success with fd{$request->fd}\n";
        });
        $server->on('message', function (\swoole_websocket_server $server, $frame) {
            echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n";
            $server->push($frame->fd, $frame->data." - This message is from swoole websocket server.");
        });
        $server->on('close', function ($ser, $fd) {
            echo "client {$fd} closed\n";
        });
        $server->start();
    }
}